// pages/login/login.js
import Dialog from '@vant/weapp/dialog/dialog';
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    username: "",
    password: "",
    userInfo: {},
    remember: true,
    autoLogin: false,
  },
  inputUserName(e) {
    console.log(e.detail.value)
  },
  inputPassword(e) {
    console.log(e.detail.value)

  },
  loginUsePssword() {

    //判断用户名密码是否为空
    if (this.data.username.length == 0 || this.data.password.length == 0) {
      Dialog.alert({
        title: '登录失败',
        message: '错误的用户名或密码',
      }).then(() => {
        // on close
      });
    }
    else {
      wx.request({
        url: app.globalData.baseUrl + '/api/TokenAuth/Authenticate',
        data: {
          userName: this.data.username,
          password: this.data.password
        },
        method: "POST",
        header: {
          'content-type': 'application/json' // 默认值
        },
        success(res) {
          if (res.data.success) {
            //保存token
            app.globalData.token = res.data.result.accessToken;
            //获取用户基本信息
            wx.request({
              url: app.globalData.baseUrl + '/api/services/app/User/Get',
              data: {
                Id: res.data.result.userId
              },
              header: {
                'content-type': 'application/json', // 默认值
                'Authorization': 'Bearer ' + app.globalData.token
              },
              success(res) {
                app.globalData.userInfo = res.data.result;
                wx.navigateTo({
                  url: '/pages/index/index',
                });
              },
              fail(res) {
                console.log(res);
              }
            });
          }
        },
        fail(error) {
          console.log(error);
        }
      });
    }
  },
  userLogin(event) {
    var remember = this.data.remember;
    var autoligin = this.data.autoLogin;
    if (remember) {
      wx.setStorageSync('username', this.data.username);
      wx.setStorageSync('password', this.data.password);
      wx.setStorageSync('remember', this.data.remember);
    }
    if (autoligin) {
      wx.setStorageSync('autologin', this.data.autoLogin);
    }
    this.loginUsePssword();
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if (app.globalData.userInfo) {
      wx.navigateTo({
        url: '/pages/index/index',
      });
    }
    //是否自动登录
    var autologin = wx.getStorageSync('autologin');
    var remember = wx.getStorageSync('remember');
    var username = wx.getStorageSync('username');
    var password = wx.getStorageSync('password');
    this.setData({
      username: username,
      password: password,
      remember: remember,
      autoLogin: autologin
    });

    if (autologin) {
      this.userLogin();
    }

  },
  remeberPasswordChange(event) {
    this.setData({ remember: event.detail });
  },
  autoLoginChange(event) {
    this.setData({ autoLogin: event.detail });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})
